package com.mydemo;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Start {

    public static int startTime;
    public static int endTime;
    public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    public static String path;
    public static String descPath;

    public static void main(String[] args) throws IOException {

        if (args.length<2){
            System.out.println("program require two params");
            return;
        }
        path = args[0];
        File file = new File(path);
        if (!file.exists()){
            System.out.println("param1 is not a file or dir");
            return;
        }
        if (path.contains(File.separator)){
            descPath = path.substring(path.lastIndexOf(File.separator)+1);
        } else {
            descPath = path.substring(path.lastIndexOf("/")+1);
        }

        String[] dates = args[1].split("~");
        startTime = Integer.parseInt(dates[0]);
        endTime = Integer.parseInt(dates[1]);

        copyFile(path);

    }

    //拷贝文件，文件夹均可，如果是文件则目标地址所包含文件夹要存在
    public static void copyFile(String sourPath){

        File file = new File(sourPath);
        if (file.isFile()){
            //按日期校验，通过则拷贝，不通过则忽略
            String desPath = getDescPath(sourPath);

            if (dateCheck(file,desPath)){
                //通过校验，获得目标路径
                //创建目录
                String descDir = getDescDir(desPath);
                new File(descDir).mkdirs();
                try {
                    InputStream inputStream = new FileInputStream(sourPath);
                    OutputStream outputStream = new FileOutputStream(desPath);
                    byte[] bytes = new byte[100];
                    int i = -1;
                    while ((i=inputStream.read(bytes))!=-1){
                        outputStream.write(bytes,0,i);
                    }
                    inputStream.close();
                    outputStream.close();
                    //修改最后更新时间
                    File outFile = new File(desPath);
                    outFile.setLastModified(file.lastModified());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

        if (file.isDirectory()){

            File[] files = file.listFiles();
            for (File subFile:files){
                copyFile(subFile.getAbsolutePath());
            }
        }

    }

    //校验日期，在范围内通过，不在范围内忽略
    public static boolean dateCheck(File file,String desPath){

        int date = Integer.parseInt(format.format(new Date(file.lastModified())));
        if (date>=startTime&&date<=endTime){
            System.out.println(file.getAbsolutePath()+"   "+date+"   "+"CopyTo "+desPath);
            return true;
        } else {
            System.out.println(file.getAbsolutePath()+"   "+date+"   "+"Ignore");
            return false;
        }
    }

    //获得目标路径
    public static String getDescPath(String sourPath){

        String desPath = descPath + sourPath.substring(path.length());

        return desPath;
    }

    //获得目标目录
    public static String getDescDir(String desPath){

        String descDir = desPath.substring(0,desPath.lastIndexOf(File.separator));

        return descDir;
    }


}
